package com.webull.information.center.carwler.task.handler;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.function.Function;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.webull.information.center.carwler.common.model.NewsInformation;
import com.webull.information.center.carwler.common.model.NewsInformation.ArticleClass;
import com.webull.information.center.carwler.common.util.news.R1CaiNewsListUitl;
import com.webull.information.center.carwler.common.util.news.R1CaiNewsListUitl.R1CaiItem;
import com.webull.information.center.carwler.task.support.TikcerNewsProcessor;
import com.webull.information.center.common.constants.Constants;
import com.webull.information.client.model.News;
import com.webull.information.client.model.parm.NewsRelType;

/**
 * 
 * @author smj
 * @Date 2017年7月7日上午9:07:53
 * @version 1.0
 * 
 *          华尔街见闻新闻处理器
 */
@Component
public class R1CaiJinNewsHandler {

	protected final Logger logger = LogManager.getLogger(getClass());
	private static String r1Cai_lang = Constants.lang_zh;// 新浪语言版本
	private @Autowired R1CaiNewsListUitl r1CaiNewsListUitl;
	private @Autowired TikcerNewsProcessor tikcerNewsProcessor;

	public void handlerR1CaiJinnCnNews() {

		/*
		 * step1 获取banner页面，当做 frontPage
		 * 
		 */
		List<R1CaiItem> parseTopNewsList = r1CaiNewsListUitl.parseNewsList();
		processR1CaiList(parseTopNewsList);

	}


	private void processR1CaiList(List<R1CaiItem> wallItems) {
		if (wallItems != null)
			for (R1CaiItem item : wallItems) {
				try {
					processR1CaiNews(item);
				} catch (Exception e) {
					logger.error("processWallList wallItems url:{}, error", item.getLink(), e);
				}
			}

	}

	/**
	 * 绑定属性
	 * 
	 * @param source
	 * @param info
	 */
	private void bindProperties(R1CaiItem source, NewsInformation info) {
		info.setTitle(source.getTitle());
		info.setNewsTime(source.getNewsTime());
		info.setUrl(source.getLink());
		info.setSourceName(source.getSource());
		info.setSummary(source.getSummry());
		info.setHomePage(source.getHomeType());
		info.addMainPic(source.getImg());
		info.setMainProxy(false);
		if (News.HomeType.frontPage.equals(info.getHomePage())) {
			info.getMainPic().setThumSize(450);
		}

		info.setRegionId(1);
		info.setLanguage(r1Cai_lang);
		info.setCollectSource(News.CollectSource.R1Cai);
		info.setSiteType(News.SiteType.NORMAL);
		info.setArticleClass(ArticleClass.article);
	}


	private void processR1CaiNews(R1CaiItem item) throws Exception {

		NewsInformation info = new NewsInformation();// produce(doc);

		bindProperties(item, info);

		if (!repeatArticle.apply(info.getUrl()) || News.HomeType.frontPage.equals(item.getHomeType())) {
			/*
			 * 填充内容和关联新闻
			 */
			tikcerNewsProcessor.bindTargetNewsRecord(info, 0, NewsRelType.homePage);
		}
	}

	private Function<String, Boolean> repeatArticle = new Function<String, Boolean>() {
		private LinkedList<String> ids = new LinkedList<>();

		@Override
		public Boolean apply(String id) {
			if (ids.contains(id))
				return true;
			// 未重复
			if (ids.size() > 200) {
				String id0 = (String) (((Queue) ids).poll());
				ids.add(id);
			}
			return false;
		}



	};



}
